/**
 * Copyright (C) 2013-2014 the original author or authors.
 */
package com.foreveross.bsl.mdm.ios.application.impl;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import javax.inject.Named;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageImpl;
import org.springframework.data.domain.Pageable;

import com.foreveross.bsl.common.utils.bean.BeanHelper;
import com.foreveross.bsl.common.utils.enums.OperatEnum;
import com.foreveross.bsl.mdm.ios.application.MdmTaskApplication;
import com.foreveross.bsl.mdm.ios.application.vo.MdmTaskVo;
import com.foreveross.bsl.mdm.model.ios.domain.MdmTask;

/**
 * @description
 *
 * @author heyaowen
 * @version v1.0
 *
 * @2014-7-18
 *
 */
@Named("iosMdmTaskApplication")
public class MdmTaskApplicationImpl implements MdmTaskApplication {

	/*
	 * (non-Javadoc)
	 *
	 * @see
	 * com.foreveross.bsl.system.application.DatabaseInfoApplication#saveEntity
	 * (com.foreveross.bsl.system.application.vo.DatabaseInfoVO)
	 */
	@Override
	public MdmTaskVo saveEntity(MdmTaskVo entity) {
		// 转换成领域对象
		MdmTask task = new MdmTask();
		BeanHelper.copyProperties(task, entity);
		task.save();
		entity.setId(task.getId());
		return entity;
	}

	/*
	 * (non-Javadoc)
	 *
	 * @see
	 * com.foreveross.bsl.system.application.DatabaseInfoApplication#updateEntity
	 * (com.foreveross.bsl.system.application.vo.DatabaseInfoVO)
	 */
	@Override
	public MdmTaskVo updateEntity(String id, MdmTaskVo entity) {
		MdmTask task = new MdmTask(); // 防止可能为null
		task = (MdmTask) task.findOne(id);
		BeanHelper.copyProperties(task, entity);
		task.save();
		BeanHelper.copyProperties(entity, task);
		return entity;
	}

	/*
	 * (non-Javadoc)
	 *
	 * @see
	 * com.foreveross.bsl.system.application.DatabaseInfoApplication#deleteEntity
	 * (com.foreveross.bsl.system.application.vo.DatabaseInfoVO)
	 */
	@Override
	public void deleteEntity(String id) {
		MdmTask task = new MdmTask();
		task = (MdmTask) task.findOne(id);
		task.delete();
	}

	/*
	 * (non-Javadoc)
	 *
	 * @see
	 * com.foreveross.bsl.system.application.DatabaseInfoApplication#getEntity
	 * (java.lang.String)
	 */
	@Override
	public MdmTaskVo getEntity(String id) {
		MdmTask task = new MdmTask();
		task = (MdmTask) task.findOne(id);
		MdmTaskVo vo = new MdmTaskVo();
		BeanHelper.copyProperties(vo, task);
		return vo;
	}

	/*
	 * (non-Javadoc)
	 *
	 * @see
	 * com.foreveross.bsl.system.application.DatabaseInfoApplication#getEntity
	 * (java.lang.String)
	 */
	@Override
	public List<MdmTaskVo> getEntityByApnsToken(String apnsToken) {
		MdmTask task = new MdmTask();
		List<MdmTask> tasks = (List<MdmTask>) task.findListByProperty("apnsToken", OperatEnum.EQ, apnsToken);
		if (tasks != null) {
			List<MdmTaskVo> result = new ArrayList<MdmTaskVo>();
			MdmTaskVo vo;
			for (Iterator iterator = tasks.iterator(); iterator.hasNext();) {
				MdmTask obj = (MdmTask) iterator.next();
				vo = new MdmTaskVo();
				BeanHelper.copyProperties(vo, obj);
				result.add(vo);
			}
			return result;
		}
		return null;
	}
	
	/*
	 * (non-Javadoc)
	 *
	 * @see
	 * com.foreveross.bsl.system.application.DatabaseInfoApplication#getEntity
	 * (java.lang.String)
	 */
	@Override
	public List<MdmTaskVo> getEntityByToken_status(String apnsToken, String status) { // 确保apnsToken是唯一
		MdmTask task = new MdmTask();
		OperatEnum[] operats = {OperatEnum.EQ, OperatEnum.EQ};
		String[] proNames = {"apnsToken", "status"};
		String[] values = {apnsToken, status};
		List<MdmTask> tasks = (List<MdmTask>)task.findListByProperty(proNames, operats, values);
		if (tasks != null) {
			List<MdmTaskVo> result = new ArrayList<MdmTaskVo>();
			MdmTaskVo vo;
			for (Iterator iterator = tasks.iterator(); iterator.hasNext();) {
				MdmTask obj = (MdmTask) iterator.next();
				vo = new MdmTaskVo();
				BeanHelper.copyProperties(vo, obj);
				result.add(vo);
			}
			return result;
		}
		return null;
	}
	
	/*
	 * (non-Javadoc)
	 *
	 * @see
	 * com.foreveross.bsl.system.application.DatabaseInfoApplication#getEntity
	 * (java.lang.String)
	 */
	@Override
	public List<MdmTaskVo> getEntityByToken_status_commandUUID(String apnsToken, 
			String status, String commandUUID) { // 确保apnsToken是唯一
		MdmTask task = new MdmTask();
		OperatEnum[] operats = {OperatEnum.EQ, OperatEnum.EQ, OperatEnum.EQ};
		String[] proNames = {"apnsToken", "status", "commandUUID"};
		String[] values = {apnsToken, status, commandUUID};
		List<MdmTask> tasks = (List<MdmTask>)task.findListByProperty(proNames, operats, values);
		if (tasks != null) {
			List<MdmTaskVo> result = new ArrayList<MdmTaskVo>();
			MdmTaskVo vo;
			for (Iterator iterator = tasks.iterator(); iterator.hasNext();) {
				MdmTask obj = (MdmTask) iterator.next();
				vo = new MdmTaskVo();
				BeanHelper.copyProperties(vo, obj);
				result.add(vo);
			}
			return result;
		}
		return null;
	}

	/*
	 * (non-Javadoc)
	 *
	 * @see
	 * com.foreveross.bsl.system.application.DatabaseInfoApplication#findAll()
	 */
	@Override
	public List<MdmTaskVo> findAll() {
		MdmTask task = new MdmTask();
		Iterable iterable = task.findAll();
		if (iterable != null) {
			List<MdmTaskVo> result = new ArrayList<MdmTaskVo>();
			MdmTaskVo vo;
			for (Iterator iterator = iterable.iterator(); iterator.hasNext();) {
				MdmTask obj = (MdmTask) iterator.next();

				vo = new MdmTaskVo();
				BeanHelper.copyProperties(vo, obj);
				result.add(vo);
			}
			return result;
		}
		return null;
	}

	/*
	 * (non-Javadoc)
	 *
	 * @see
	 * com.foreveross.bsl.system.application.DatabaseInfoApplication#findByPageable
	 * (org.springframework.data.domain.Pageable)
	 */
	@Override
	public Page<MdmTaskVo> findByPageable(Pageable pageable) {
		MdmTask task = new MdmTask();
		Page page = task.findAll(pageable);
		if (page != null && page.getContent() != null) {
			List<MdmTaskVo> result = new ArrayList<MdmTaskVo>();
			MdmTaskVo vo;
			for (Iterator iterator = page.getContent().iterator(); iterator
					.hasNext();) {
				MdmTask obj = (MdmTask) iterator.next();

				vo = new MdmTaskVo();
				BeanHelper.copyProperties(vo, obj);
				result.add(vo);
			}
			Page<MdmTaskVo> pageVO = new PageImpl<MdmTaskVo>(result, pageable,
					page.getTotalElements());
			return pageVO;
		}
		return null;
	}

	/*
	 * (non-Javadoc)
	 *
	 * @see
	 * com.foreveross.bsl.system.application.MdmTaskApplication#findByPageable
	 * (org.springframework.data.domain.Pageable,
	 * com.foreveross.bsl.system.application.vo.MdmTaskVo)
	 */
	@Override
	public Page<MdmTaskVo> findByPageable(Pageable pageable, MdmTaskVo MdmTask) {
		MdmTask task = new MdmTask();
		BeanHelper.copyProperties(task, MdmTask);
		Page page = task.findByPageable(pageable, task);
		if (page != null && page.getContent() != null) {
			List<MdmTaskVo> result = new ArrayList<MdmTaskVo>();
			MdmTaskVo vo;
			for (Iterator iterator = page.getContent().iterator(); iterator.hasNext();) {
				MdmTask obj = (MdmTask) iterator.next();

				vo = new MdmTaskVo();
				BeanHelper.copyProperties(vo, obj);
				result.add(vo);
			}
			Page<MdmTaskVo> pageVO = new PageImpl<MdmTaskVo>(result, pageable,
					page.getTotalElements());
			return pageVO;
		}
		return null;
	}
}
